package tuilayer;
import java.util.*;
import controlLayer.*;
import modelLayer.*;

public class LoanUI {
    private static boolean valid;
    private PersonCtr personCtr;
    private LoanCtr loanCtr;
    private DVDCtr dvdCtr;

    public LoanUI() {
        personCtr = new PersonCtr();
        loanCtr = new LoanCtr();
        dvdCtr = new DVDCtr();
    }

    public void handleLoan(){
        mainMenuLoan();
    }
    
    private void mainMenuLoan() {
        boolean stop = false;
        while (!stop) {
            int choice = writeMainMenuLoan();
            switch (choice) {
                case 0:
                    stop = true;
                    break;
                case 1:
                    createLoan();
                    break;
                case 2:
                    findLoan();
                    break;
                case 3:
                    extendLoan();
                    break;
                case 4:
                    deleteLoan();
                    break;
                case 5:
                    completeLoan();
                    break;
                default: System.out.println("Please enter a valid choice.");
            }
        }  
    }
    
    private int writeMainMenuLoan() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\n*****Loan Menu*****");
        System.out.println("(0) Back");
        System.out.println("(1) Create Loan");
        System.out.println("(2) Find Loan");
        System.out.println("(3) Extend Loan");
        System.out.println("(4) Delete Loan");
        System.out.println("(5) Complete Loan");
        System.out.println("\nType your choice:");
        
        int choice = ValidateInput.readInt("Please enter a valid choice.");
        
        if(ValidateInput.validateInt(choice))        
            return choice;
        else {
            return 999;
        }
    }
    
    private void createLoan() {
        Scanner keyboard = new Scanner(System.in);
        
        System.out.println("\nType the borrow date:");
        String borrowDate = keyboard.nextLine();
        if(ValidateInput.isInteger(borrowDate))
            if(Integer.parseInt(borrowDate) == 0)
                return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validateBorrowDate(borrowDate))
                valid = true;
            else {
                System.out.println("\nPlease provide valid input.");
                System.out.println("Type the borrow date:");
                borrowDate = keyboard.nextLine();
            }
        }
        
        System.out.println("\nType the period:");
        String period = keyboard.nextLine();
        if(ValidateInput.isInteger(period))
            if(Integer.parseInt(period) == 0)
                return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validateBorrowDate(period))
                valid = true;
            else {
                System.out.println("\nPlease provide valid input.");
                System.out.println("Type the period:");
                period = keyboard.nextLine();
            }
        }
        
        System.out.println("\nType the person's id:");
        int id = ValidateInput.readInt("Type the person's id:");
        if(id == 0) return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validateID(id))
                valid = true;
            else {
                System.out.println("\nThis id does not exist.");
                System.out.println("Type the person's id:");
                id = ValidateInput.readInt("Type the person's id:");
            }
        }
        
        System.out.println("\nType the DVD copy's serial number:");
        int serialNumber = ValidateInput.readInt("Type the DVD copy's serial number:");
        if(serialNumber == 0) return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validateDVDCopy(serialNumber) && ValidateInput.validateDVDCopyAvailability(serialNumber))
                valid = true;
            else {
                System.out.println("\nThis serial number does not exist or this copy is already borrowed.");
                System.out.println("Type the DVD copy's serial number::");
                serialNumber = ValidateInput.readInt("Type the DVD copy's serial number:");
            }
        }
        
        loanCtr.createLoan(borrowDate, period, personCtr.getPerson(id), dvdCtr.findDVDCopy(serialNumber));
        
        System.out.println("\nLoan successfully created!");
    }
    
    private void findLoan() {
        Scanner keyboard = new Scanner(System.in);
        
        System.out.println("\nType the loan's id:");
        int id = ValidateInput.readInt("Type the loan's id:");
        if(id == 0) return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validateLoanID(id))
                valid = true;
            else {
                System.out.println("\nThis id does not exist.");
                System.out.println("Type the loan's id:");
                id = ValidateInput.readInt("Type the loan's id:");
            }
        }
        
        Loan loan = loanCtr.findLoan(id);
        System.out.println();
        System.out.println("Borrow date:" + loan.getBorrowDate());
        System.out.println("Period:" + loan.getPeriod());
        System.out.println("State:" + loan.getState());
        System.out.println("Person's ID:" + loan.getPerson().getID());
        System.out.println("Copy's serial number" + loan.getCopy().getSerialNumber());
    }
    
    private void extendLoan() {
        Scanner keyboard = new Scanner(System.in);
        
        System.out.println("\nType the loan's id:");
        int id = ValidateInput.readInt("Type the loan's id:");
        if(id == 0) return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validateLoanID(id))
                valid = true;
            else {
                System.out.println("\nThis id does not exist.");
                System.out.println("Type the loan's id:");
                id = ValidateInput.readInt("Type the loan's id:");
            }
        }
        
        System.out.println("\nType the new period:");
        String period = keyboard.nextLine();
        if(ValidateInput.isInteger(period))
            if(Integer.parseInt(period) == 0)
                return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validateBorrowDate(period))
                valid = true;
            else {
                System.out.println("\nPlease provide valid input.");
                System.out.println("Type the new period:");
                period = keyboard.nextLine();
            }
        }
        
        Loan loan = loanCtr.findLoan(id);
        loan.setPeriod(period);
        
        System.out.println("\nLoan successfully updated!");
    }
    
    private void deleteLoan() {
        Scanner keyboard = new Scanner(System.in);
        
        System.out.println("\nType the loan's id:");
        int id = ValidateInput.readInt("Type the loan's id:");
        if(id == 0) return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validateLoanID(id))
                valid = true;
            else {
                System.out.println("\nThis id does not exist.");
                System.out.println("Type the loan's id:");
                id = ValidateInput.readInt("Type the loan's id:");
            }
        }
        
        loanCtr.deleteLoan(id);
        
        System.out.println("\nLoan successfully deleted!");
    }
    
    private void completeLoan() {
        Scanner keyboard = new Scanner(System.in);
        
        System.out.println("\nType the loan's id:");
        int id = ValidateInput.readInt("Type the loan's id:");
        if(id == 0) return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validateLoanID(id))
                valid = true;
            else {
                System.out.println("\nThis id does not exist.");
                System.out.println("Type the loan's id:");
                id = ValidateInput.readInt("Type the loan's id:");
            }
        }
        
        loanCtr.completeLoan(id);
        
        System.out.println("\nLoan marked as complete!");
        
    }
}
